#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QLabel>
#include <QTcpSocket>
#include  <QTcpServer>
#include <QPlainTextEdit>
#include "adduserdialog.h"
QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    MainWindow(QWidget *parent = nullptr);
    ~MainWindow();

private slots:
    void onSocketReadyRead();
    void onNewsocket();
private slots:
    void on_pb_addUser_clicked();

    void on_toolButton_clicked();

    void on_toolButton_2_clicked();

    void on_toolButton_6_clicked();

    void on_toolButton_5_clicked();

    void on_toolButton_4_clicked();

    void on_pb_goMain_clicked();

    void on_pb_start_servers_clicked();

    void on_pushButton_2_clicked();

    void on_pb_after_clicked();

    void on_pushButton_clicked();

    void on_pushButton_3_clicked();

    void on_pb_query_clicked();

    void on_pushButton_4_clicked();

    void on_pushButton_7_clicked();

    void on_pushButton_8_clicked();

    void on_pushButton_5_clicked();

    void on_pushButton_6_clicked();

    void on_toolButton_7_clicked();

    void on_pb_conec_dev_clicked();

    void on_pb_dev_control_1_clicked();

    void on_pb_dev_control_2_clicked();

private:
    addUserDialog *adduser_dialog;
    Ui::MainWindow *ui;
    QLabel * labSocketStat;
    QTcpServer *tcpServer; //TCP服务器
     QTcpSocket *tcpSocket;//
    void getLoadIP();

     QList<QPair<QTcpSocket *,bool > > UserList;
     void DataProcessing(QByteArray Data , QTcpSocket *TmptcpSocket);
     uint getLoadTime();
     uint getFormatTime(QString data, QString format);

     int up_ph;
     int up_temperature;
     int up_waterQuantity;
     int up_WaterMargin;
     int down_ph;
     int down_temperature;
     int down_waterQuantity;
     int down_WaterMargin;

     QList <QPair<int,int>>realDatalist;
     void UserLogin(QString username, QString userPassword, QTcpSocket *TmptcpSocket);
     void connectTCP(QString ip, int port);
       QTcpSocket *tcpClient;//底层连接的指针
    QPlainTextEdit * edit_debug;

    bool idconnectDev ;
    bool realdataRead;
    void send_warning(QString info);
public:
     QString nowAccout;
     QString nowPassword;
     int userType;
     void updateuserInfo();
private:
     void addLog(QString s)
     {
         if (edit_debug != NULL)
         {
             edit_debug->appendPlainText(s);
         }
         else
         {
             qDebug()<<s;
         }
     }

     void addLog(const char *fmt,...)
     {
         char buf[2024];
         va_list va;
         va_start(va,fmt);
         vsprintf(buf,fmt,va);
         va_end(va);
         if (edit_debug != NULL)
         {
             edit_debug->appendPlainText(buf);
         }
         else
         {
             qDebug()<<buf;
         }
     }
     void login(QString username, QString password, QTcpSocket *TmptcpSocket);
     void analysis(QString dat, QTcpSocket *TmptcpSocket);
     void Regist(QString username, QString password, QTcpSocket *TmptcpSocket);
};
#endif // MAINWINDOW_H
